package org.jeecg.modules.inOrderRules.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.inOrderRules.entity.InOrderRules;
import org.jeecg.modules.inOrderRules.service.IInOrderRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 上架策略
 * @Author: jeecg-boot
 * @Date: 2023-11-16
 * @Version: V1.0
 */
@Api(tags = "上架策略")
@RestController
@RequestMapping("/inOrderRules/inOrderRules")
@Slf4j
public class InOrderRulesController extends JeecgController<InOrderRules, IInOrderRulesService> {
    @Autowired
    private IInOrderRulesService inOrderRulesService;

    /**
     * 分页列表查询
     *
     * @param inOrderRules
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "上架策略-分页列表查询")
    @ApiOperation(value = "上架策略-分页列表查询", notes = "上架策略-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(InOrderRules inOrderRules,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
//        QueryWrapper<InOrderRules> queryWrapper = QueryGenerator.initQueryWrapper(inOrderRules, req.getParameterMap());
        LambdaQueryWrapper<InOrderRules> lqw = Wrappers.lambdaQuery(InOrderRules.class);
        if (StringUtils.isNotBlank(inOrderRules.getOwnerName())) {
            lqw.like(InOrderRules::getOwnerName, inOrderRules.getOwnerName());
        }
        if (StringUtils.isNotBlank(inOrderRules.getRulesName())) {
            lqw.like(InOrderRules::getRulesName, inOrderRules.getRulesName());
        }
        Page<InOrderRules> page = new Page<InOrderRules>(pageNo, pageSize);
        IPage<InOrderRules> pageList = inOrderRulesService.page(page, lqw);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param inOrderRules
     * @return
     */
    @AutoLog(value = "上架策略-添加")
    @ApiOperation(value = "上架策略-添加", notes = "上架策略-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody InOrderRules inOrderRules) {
        inOrderRulesService.save(inOrderRules);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param inOrderRules
     * @return
     */
    @AutoLog(value = "上架策略-编辑")
    @ApiOperation(value = "上架策略-编辑", notes = "上架策略-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody InOrderRules inOrderRules) {
        inOrderRulesService.updateById(inOrderRules);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "上架策略-通过id删除")
    @ApiOperation(value = "上架策略-通过id删除", notes = "上架策略-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        inOrderRulesService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "上架策略-批量删除")
    @ApiOperation(value = "上架策略-批量删除", notes = "上架策略-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.inOrderRulesService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "上架策略-通过id查询")
    @ApiOperation(value = "上架策略-通过id查询", notes = "上架策略-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        InOrderRules inOrderRules = inOrderRulesService.getById(id);
        if (inOrderRules == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(inOrderRules);
    }

    /**
     * 根据货主编码查询上架策略
     *
     * @param onwerCode
     * @return
     */
    public InOrderRules queryByOwnerCode(String onwerCode) {
        LambdaQueryWrapper<InOrderRules> lqw = Wrappers.lambdaQuery(InOrderRules.class);
        lqw.eq(InOrderRules::getOwnerCode, onwerCode);
        return inOrderRulesService.getOne(lqw);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param inOrderRules
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, InOrderRules inOrderRules) {
        return super.exportXls(request, inOrderRules, InOrderRules.class, "上架策略");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, InOrderRules.class);
    }

}
